package com.supermarket.shop.system.controller;

import com.supermarket.shop.system.domain.Commodity;
import com.supermarket.shop.system.domain.Vip;
import com.supermarket.shop.system.service.ServiceVip;
import com.supermarket.shop.system.service.impl.ServiceVipImpl;

import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
//用户界面
public class VipController {
    ServiceVip serviceVip=new ServiceVipImpl();
    Scanner sc = new Scanner(System.in);
    public void start() throws IOException {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <VIP会员> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加VIP会员  2.删除VIP会员  3.修改VIP会员  4.查询VIP会员  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加功能");
                    addVip();
                    break;
                case "2":
                    System.out.println("删除功能");
                    deleteVipByName();
                    break;
                case "3":
                    System.out.println("修改功能");
                    updateVipByname();
                    break;
                case "4":
                    System.out.println("查询功能");
                    findAllVip();
                    break;
                case "5":
                    System.out.println("感谢您使用Vip会员管理系统, 再见!");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    //重新设置指定VIP用户的所有信息
    private void updateVipByname() throws IOException {
        String name=nameVip(true);
        if(name.equals("null")){
            return;
        }
        System.out.println("(如果不想修改名称输入0)");
        String nameupdate;
        if(sc.next().equals("0")){
            nameupdate=name;
        }else{
            System.out.println("请输入修改后VIP会员名称:");
            nameupdate=nameVip(false);
        }
        if(name==null){
            System.out.println("退出:您可以选择其他操作!");
            return;
        }
        Vip vip=inputVip(name);
        serviceVip.updateVipByname(vip,nameupdate);
    }

    //删除指定VIP用户名称的会员信息
    private void deleteVipByName() throws IOException {
        System.out.println("请输入VIP会员用户名称:");
        String name=nameVip(true);
        if(name==null){
            System.out.println("退出:您可以选择其他操作!");
            return;
        }
        serviceVip.deleteVipByName(name);
    }

    //查找全部的VIP会员用户
    private void findAllVip() throws IOException {
        serviceVip.findAllVip();
    }

    //增加一个VIP会员用户
    private void addVip() throws IOException {
        String name = nameVip(false);
        if(name==null){
            System.out.println("退出:您可以选择其他操作!");
            return;
        }
       Vip vip=inputVip(name);
        boolean bl=serviceVip.addVip(vip);
        System.out.println("添加成功");
    }

    //录入一个VIP会员用户的信息
    private Vip inputVip(String name) throws IOException {
        Vip vip = new Vip();
        vip.setName(name);
        System.out.println("输入电话号码:");
        vip.setPhone(sc.next());
        System.out.println("输入密码:");
        vip.setPass(sc.next());
        System.out.println("自动生成当前日期");
        vip.setDate(new Date());
        System.out.println("自动生成商品序号");
        vip.setId(serviceVip.maxCommodityId() + 1);
        return vip;
    }

    //输入一个用户名称确定他不在VIP会员用户里面
    private String nameVip(boolean flag) throws IOException {
        Scanner sc = new Scanner(System.in);
        String name = null;
        while (true) {
            System.out.println("(如果你想退出去可以输入null)请输入商品VIP会员名称:");
            name = sc.next();
            if(name.equals("null")){
                break;
            }
            boolean bl = serviceVip.isExist(name);
            System.out.println(bl);
            System.out.println(!bl^flag);
            if ((!bl)^flag) {
                break;
            }
            System.out.println("您输入的信息不正确,请您重新输入:");
        }
        return name;
    }


}
